package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.Observable;
import java.util.Observer;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.FileParser;
import model.Manuscript;
import model.Review;
import model.State;
import model.Status;
import model.User;

@SuppressWarnings("serial")
/**
 * Panel specific to user type SPC. Shows papers 
 * assigned to SPC and reviews completed by reviewers.
 * Also, shows recommendations made by SPC.
 * @author Kevin Anderson, Jeff Kuhlman, Sarah George
 */
public class SPCPanel extends JPanel implements Observer {
	
	/*
	 * Current state of user, including user id, conference,
	 * and role.
	 */
	private State my_state;
	
	/*
	 * File parser object.
	 */
	private FileParser my_fp;
	
	/*
	 * Papers associated with a conference.
	 */
	private Manuscript[] my_papers;
	
	private static final String[] headTitles = new String[] 
			{ "Title","Open Paper","Paper Status","Reviews", "Get Rev","Assign","Recommend" };
	private static final int cols = headTitles.length;

	
	public SPCPanel(State the_state) {
		super();
		my_state = the_state;
		my_fp = my_state.getParser();
		my_fp.addObserver(this);
		setup();
	}
	
	public void setup() {
		
		this.setLayout(new GridLayout(0,7));
		//GET A LIST OF PAPERS ASSIGNED TO THE SPC
		FileParser fp = my_state.getParser();
		my_papers = fp.getPapersBySPC(my_state.getUser().getID(), my_state.getConference().getConfID());
		
		//FOR EACH PAPER DO SOMETHING
		if(my_papers.length == 0) {
			JOptionPane.showMessageDialog(this, "You not have any papers assigned to you for this conference.  Please contact your Program Chair");
		} else {
			
			//PREPARE OBJECTS TO DRAW
			if (my_papers.length > 0) //draw headers
			{
				JLabel[] headers = new JLabel[cols];
				for(int i = 0; i < cols; i++)
				{
					headers[i] = new JLabel(headTitles[i]);
					headers[i].setHorizontalAlignment(JLabel.CENTER);
					add(headers[i]);
				}
			}

			
			for (int i = 0; i < my_papers.length; i++)
			{
				add(new JLabel(my_papers[i].getMy_title()));
				add(new JButton(new OpenPaperAction(my_papers[i])));
				add(new JLabel(my_papers[i].getMy_status().toString()));
				buildComboBox(my_papers[i]);
				add(new JButton(new AddReviewerAction(my_papers[i])));
				
				JButton reco_button = new JButton();
				
				if(my_papers[i].getMy_status() == Status.REVIEWED)
				{
					reco_button.addActionListener(new AddRecommendationAction(my_papers[i]));
					reco_button.setText("+Reco");
				} else {
					reco_button.setEnabled(false);
					reco_button.setText("*"+my_papers[i].getMy_status()+"*");
				}
				
				add(reco_button);
			}
			this.setVisible(true);
		}
	}
	
	private void clear()
	{
		this.removeAll();
	}


	private void buildComboBox(Manuscript the_paper)
	{
		JComboBox<User> combo = new JComboBox<User>();
		
		FileParser fp = my_state.getParser();
		for(User u : fp.getReviewersByPaperID(the_paper.getMy_id()))
		{
			combo.addItem(u);
		}
		
		JButton button = new JButton(new GetReviewAction(combo, the_paper));

		if (combo.getItemCount() == 0)
		{
			combo.enableInputMethods(false);
			button.setEnabled(false);
			button.setText("No Reviewers");
		}

		add(combo);
		add(button);
	}
	
	private class GetReviewAction extends AbstractAction
	{

		private JComboBox<User> my_combo;
		private Manuscript my_manuscript;
		
		public GetReviewAction(JComboBox<User> combo, Manuscript manuscript)
		{
			super("Get Review");
			my_combo = combo;
			my_manuscript = manuscript;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			
			int reviewerID = ((User) my_combo.getSelectedItem()).getID();
			FileParser fp = my_state.getParser();
			Review[] reviews = fp.getReviewsByPaperID(my_manuscript.getMy_id());
			
			
			if (reviews.length > 0)
			{
				//THIS IS A BAD WAY TO DO THIS, BUT BASICALLY I WANT TO SEE IF THE REVIEW WAS SUBMITTED FOR THIS PAPER.
				int openIndex = -1;
				for (int i = 0; i < reviews.length; i++)
				{
					if (reviews[i].getMy_reviewerID() == reviewerID)
					{
						openIndex = i;
					}
				}
				
				if (openIndex >= 0)
				{
					reviews[openIndex].open();
				} else {
					JOptionPane.showMessageDialog(SPCPanel.this, "This user Has Not Completed their review.");
				}
				
			} else {
				JOptionPane.showMessageDialog(SPCPanel.this, "No Reviews were found");
			}
		}
		
	}
	

	private class AddReviewerAction extends AbstractAction
	{
		private Manuscript my_paper;
		
		public AddReviewerAction(Manuscript the_paper)
		{
			super("+Reviewer");
			my_paper = the_paper;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			AddReviewerFrame reviewFrame = new AddReviewerFrame(my_paper, my_state);
		}
	}


	@Override
	public void update(Observable o, Object arg) {
		clear();
		repaint();
		setup();
		validate();
		repaint();
	}
	
	
	private class AddRecommendationAction extends AbstractAction
	{
		private Manuscript my_paper;
		
		public AddRecommendationAction(Manuscript the_paper)
		{
			super("+Reco");
			my_paper = the_paper;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			AddRecommendationFrame recommendationFrame = new AddRecommendationFrame(my_paper, my_state);
			recommendationFrame.setup();
		}
	}
}